package servlet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import objects.TranslationObject;


/**
 * Servlet implementation class TranslateServlet
 */
public class TranslateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		Default language is dk
		String lang = "";
		if(null != request.getParameter("language")) {
			lang = request.getParameter("language");
		} else {
			lang = "dk";
		}
		request.getSession().setAttribute("language", lang);
		request.getSession().setAttribute("isCorrectTranslated", "");
		Scanner scanner = null;
		try {
//			Later for .war file: ...new FileInputStream(this.getClass().getResource("/myimage.gif"));
			scanner = new Scanner( new FileInputStream("Resources/words." + lang) );			
			scanner.useDelimiter(";");			
		} catch (FileNotFoundException e) {
//			System.err.println("File not found!");
		}
		ArrayList<TranslationObject> textList = new ArrayList<TranslationObject>(); 
		while( scanner.hasNextLine()) {
			textList.add(new TranslationObject(scanner.next(), scanner.next()));
		}
		int randomNumber = (int) (Math.random() * textList.size());
		String randomText = textList.get(randomNumber).getGivenText();
//		Es muss auf jeden Fall eine Vokabel an den user ausgegeben werden.
		request.getSession().setAttribute("givenText", randomText);
		
		if(null != request.getParameter("translatedText") && null != request.getParameter("givenText")) {
			String givenText = request.getParameter("givenText");
			String translatedText = request.getParameter("translatedText");
//			Wort wurde vom user eingegeben und muss ueberprueft werden, 
//			richtig oder falsch zur session hinzugefuegt und eine neue Vokabel auch zur session hinzugefuegt.
			
			for(int i = 0; i < textList.size(); i++) {
				if(textList.get(i).getGivenText().equalsIgnoreCase(givenText) && textList.get(i).getTranslation().equalsIgnoreCase(translatedText)) {
	    			request.getSession().setAttribute("isCorrectTranslated", "Congratulations! Your translation was correct!");
	    		}
	    		else if(textList.get(i).getGivenText().equalsIgnoreCase(givenText) && !textList.get(i).getTranslation().equalsIgnoreCase(translatedText)){
	    			request.getSession().setAttribute("isCorrectTranslated", "Sorry, your translation was not correct, the right translation would have been \"" + textList.get(i).getTranslation() + "\"" );
	    		}
			}		    
		}		
		response.sendRedirect("secure/translate.jsp");		
	}
}
